/**
 * This class is for the delete and clear functionalities
 * @author Lee Li Hui
 */

package TimeSqueeze.logic;
import TimeSqueeze.common.*;
import TimeSqueeze.storage.TempStorage;

import java.util.ArrayList;

public class Delete implements CommandInterface {

	private static final String MESSAGE_TASK_DELETED = "Tasks have been deleted successfully.";
	private static final String MESSAGE_TASK_NOT_FOUND = "Tasks to be deleted are not found.";
	private static final String MESSAGE_TASKS_CLEARED = "All tasks have been removed.";
	
	private static final boolean CLEAR = true;
	private static final boolean DELETE = false;
	
	private static boolean isClearOrDelete;
	private static TaskInterface taskInterInput;
	private static Task taskInput;
	private static DeadlineTask deadlineTaskInput;
	private static TimedTask timedTaskInput;
	private static String content; //keyword from description or tag
	private static ArrayList<Integer> indexOfTimed = new ArrayList<Integer>();
	private static ArrayList<Integer> indexOfUntimed = new ArrayList<Integer>();
	private static ArrayList<Integer> indexOfDeadline = new ArrayList<Integer>();
	
	public Delete(Task task) {
		taskInput = task;
		isClearOrDelete = DELETE;
	}
	
	public Delete(DeadlineTask task) {
		deadlineTaskInput = task;
		isClearOrDelete = DELETE;
	}
	
	public Delete(TimedTask task) {
		timedTaskInput = task;
		isClearOrDelete = DELETE;
	}
	
	public Delete(boolean clear) {
		isClearOrDelete = CLEAR;
	}
	
	public Delete(TaskInterface inputTask) {
		taskInterInput = inputTask;
		
	}

	public static void delete() {
		getTaskIndex();
		
		if (!indexOfTimed.isEmpty() || !indexOfUntimed.isEmpty() || !indexOfDeadline.isEmpty()) {
			removeFromTempStorage();
			updateTextStorage();
			System.out.println(MESSAGE_TASK_DELETED);
		}
		else {
			System.out.println(MESSAGE_TASK_NOT_FOUND);
		}
	}

	private static void getTaskIndex() {
		//search untimed tasks
		for (int i = 0; i < TempStorage.untimedTasks.size(); i++) {
			if (TempStorage.untimedTasks.get(i).getDescription().contains(content) ||
					TempStorage.untimedTasks.get(i).getTag().contains(content)) {
				indexOfUntimed.add(i);
			}
		}

		//search timed tasks
		for (int i = 0; i < TempStorage.timedTasks.size(); i++) {
			if (TempStorage.timedTasks.get(i).getDescription().contains(content) ||
					TempStorage.timedTasks.get(i).getTag().contains(content)) {
				indexOfTimed.add(i);
			}
		}

		//search deadlined tasks
		for (int i = 0; i < TempStorage.deadlineTasks.size(); i++) {
			if (TempStorage.deadlineTasks.get(i).getDescription().contains(content) ||
					TempStorage.deadlineTasks.get(i).getTag().contains(content)) {
				indexOfDeadline.add(i);
			}
		}
	}

	private static void removeFromTempStorage() {
		if (!indexOfTimed.isEmpty()) {
			for (int i = indexOfTimed.size() - 1; i >= 0; i--) {
				TempStorage.deleteFromTimed(indexOfTimed.get(i));
			}
		}
		
		if (!indexOfUntimed.isEmpty()) {
			for (int i = indexOfUntimed.size() - 1; i >= 0; i--) {
				TempStorage.deleteFromUntimed(indexOfUntimed.get(i));
			}
		}
		
		if (!indexOfDeadline.isEmpty()) {
			for (int i = indexOfDeadline.size() - 1; i >= 0; i--) {
				TempStorage.deleteFromDeadline(indexOfDeadline.get(i));
			}
		}	
		
		TempStorage.updateFile();
	}

	public static void clear() {
		TempStorage.clearUntimed();
		TempStorage.clearTimed();
		TempStorage.clearDeadline();

		updateTextStorage();

		System.out.println(MESSAGE_TASKS_CLEARED);
	}

	private static void updateTextStorage(){
		return;
	}

	@Override
	public void execute() {
		if (isClearOrDelete == CLEAR) {
			clear();
		} else {
			if (taskInput != null) {
				if (taskInput.getDescription() != null) {
					content = taskInput.getDescription();
				} else {
					content = taskInput.getTag();
				}
			}
			
			if (deadlineTaskInput != null) {
				if (deadlineTaskInput.getDescription() != null) {
					content = deadlineTaskInput.getDescription();
				} else {
					content = deadlineTaskInput.getTag();
				}
			}
			
			if (timedTaskInput != null) {
				if (timedTaskInput.getDescription() != null) {
					content = timedTaskInput.getDescription();
				} else {
					content = timedTaskInput.getTag();
				}
			}
			
			delete();
			indexOfTimed.clear();
			indexOfUntimed.clear();
			indexOfDeadline.clear();
		}
	}

	@Override
	public void undo() {
		// TODO Auto-generated method stub

	}

}